#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<stdlib.h>

#define NUM 32
#define SIZE 16

int main()
{
    char* _argv[NUM] = {
        (char*)"ls",
        (char*)"-l",
        (char*)"-a",
        NULL
    };
    char* const _envp[SIZE] = {              //_envp的元素是char*类型的，_envp可以加const修饰，也可以不加
        (char*) "snowdragon=writed 666", //字符串=前面不要加空格，否则它的名字就变了,即需要加空格才是变量名
        NULL
    };
    pid_t id = fork();
    if(id == 0)
    {
        //子进程
        printf("子进程，pid:%d\n", getpid());
        sleep(1);

        //_envp是用来存储环境变量的，而不是存储选项的。选项不要加-
        execle("./mycmd", "./mycmd", "a", NULL, _envp);
        //execlp("ls", "ls", "-l", "-a", NULL);
        //execl("/usr/bin/ls", "ls", "-l", "-a", NULL);
        //execv("/usr/bin/ls", _argv);
        //execvp("ls", _argv);
        exit(1);
    }
    else
    {
        printf("父进程，pid:%d\n", getpid());
        int status = 0;
        pid_t res = waitpid(id, &status, 0);
        if(res > 0)
        {
            printf("等待子进程成功，退出码：%d\n", WEXITSTATUS(status));
        }
    }
    return 0;
}
